<template>
  <div>
   <!-- 搜索框模块 -->
   <ugo-search></ugo-search>
   <!-- 主体区域 -->
    <view class="m-category">
      <scroll-view scroll-y class="left">
        <view class="item" :class="{active:index === currentIndex}" 
        v-for="(item,index) in leftList" 
        :key="item.cat_id"
        @click="selectCate(index)">{{ item.cat_name }}</view>
      </scroll-view>
      <!-- 商品列表渲染 -->
      <!-- 只要涉及到导航跳转， 脑袋里面就要想到
      编程式导航: 给页面绑定click事件，去js里面定义方法
      声明式导航:就是在页面上使用<navigator> 这个标签 -->
      <scroll-view :scroll-top="scrollTop" scroll-y class="right">
        <view class="cate" v-for="item in rightList" :key="item.cat_id">
          <view class="title">{{ item.cat_name }}</view>
          <view class="goods-list">
            <navigator :url="`/goodsPackage/goods_list/index?query=${goods.cat_name}&cid=${goods.cat_id}`" 
            class="goods" v-for="goods in item.children" :key="goods.cat_id">
              <image :src="goods.cat_icon" class="pic" mode="aspectFit" />
              <view class="name">{{ goods.cat_name }}</view>
            </navigator>
          </view>
        </view>
      </scroll-view>
    </view>
  </div>
</template>
<script>
export default {
  name: '',
  data() {
    return {
      scrollTop:0,
      currentIndex: 0,
      CategoriesList:[]
    };
  },
  onLoad(){
    // 调用发送的请求
    this.getCategories()
  },
  computed:{
    leftList(){
      return this.CategoriesList.map(item => {
       return {
        cat_id : item.cat_id,
        cat_name : item.cat_name
       }
      })
    },
    rightList(){
      return this.CategoriesList.length>0?this.CategoriesList[this.currentIndex].children:[]
    }
  },
  methods:{
    // 发送请求
    async getCategories(){
    const res = await uni.$u.http.request({url: '/categories' });
    console.log(res.data);
    this.CategoriesList = res.data.message
    },
    // 点击选择当前项 index为遍历数组的下标
    selectCate(index){
      this.currentIndex = index;
      // 每次点击左侧菜单栏以后,动态给scrollTop设置一个值
      this.scrollTop = -1;
      // 当这个值设置完毕,等dom 更新结束以后,在动态改变为0  也就是让他在最顶部
      // 想要操作基于最新数据生成的DOM 时，就将这个操作放在 nextTick 的回调中；
      this.$nextTick(()=>{ this.scrollTop = 0 })
    }
  },
};
</script>
<style lang="scss">
.m-category {
  display: flex;
  height: calc(100vh - 44px);
  .left {
    height: 100%;
    width: 182rpx;
    font-size: 28rpx;
    text-align: center;
    background-color: #F3F3F3;
    .item {
      padding: 20rpx 0;
      &.active{
        background: #fff;
        color: #ea4350;
        position: relative;
        &::before{
          content: '';
          position: absolute;
          left: 0;
          width: 4rpx;
          height: 44rpx;
          background: #ea4350;
        }
      }
    }
  }
  .right {
    height: 100%;
    flex: 1;
    .title{
      margin: 18rpx 0 21rpx;
      text-align: center;
    }
  }
}
.goods-list{
  display: flex;
  padding: 0 48rpx;
  justify-content: space-between;
  flex-wrap: wrap;
  .pic{
    width: 120rpx;
    height: 120rpx;
  }
  .name{
    margin-bottom: 30rpx;
    font-size: 21rpx;
    color: #2E2E2E;
    text-align: center;
    line-height: 1;
  }
}
</style>
